// pages/coupon_package/coupon_send/coupon_send.js
import {
    wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: app.globalData.imgUrl,

        writeOffCouponList: [],
        writeOffCouponIndex: 0,
        writeOffCoupon: 1,

        selected: false
    },

    getList() {
        let url = '/odr/protected/voucher/findHasVouSto'
        let params = { vouId: wx.getStorageSync('vouId') }
        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + url,
            method: 'POST',
            data: params
        }, (res) => {
            for (const key in res.data) {
                this.data.writeOffCouponList[key] = res.data[key]
                this.data.writeOffCouponList[key].key = key
            }
            this.setData({
                writeOffCouponList: this.data.writeOffCouponList
            });
        })
    },

    // 使用条件类型
    pickerWriteOffPoupon: function (e) {
        var index = e.detail.value;
        var writeOffCouponList = this.data.writeOffCouponList;
        var writeOffCoupon = ''

        if (writeOffCouponList.length < 1) {
            wx.showToast({
              title: '没有核销的店铺！',
              icon: 'error'
            })
            return
        }
        writeOffCouponList.map((item, i) => {
            if (i == index) {
                writeOffCoupon = item
            }
        })
        var selected = true

        this.setData({
            writeOffCouponIndex: index,
            writeOffCoupon,
            selected
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList()
    },

    // 提交核销优惠券
    formSubmit: function (e) {
        var formdata = e.detail.value;
        let url = '/odr/protected/comm/beanCrud/mar.UserVoucherVO/update'
        let params = this.data.writeOffCoupon

        if (params === 1) {
            wx.showToast({
              title: '请选择核销店铺！',
              icon: 'error'
            })
            return;
        }

        params.used = 1;

        // 请求
        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + url,
            method: 'POST',
            data: params,
        }, (res) => {
            wx.showToast({
                title: '核销成功!',
                icon: 'success'
            })
            setTimeout(() => {
                wx.redirectTo({
                    url: '/pages/coupon_package/coupon_index',
                })
            }, 1500)
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync('vouId')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})